package com.demo.server.config;


import javax.servlet.http.HttpServletRequest;

import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

import com.demo.server.constant.CodeMsg;
import com.demo.server.util.Result;

import cn.dev33.satoken.exception.NotLoginException;
import cn.dev33.satoken.exception.NotPermissionException;
import lombok.extern.slf4j.Slf4j;

@Slf4j
@ControllerAdvice
public class GlobalExceptionHandler {
	
   @ResponseBody
   @ExceptionHandler(Exception.class)
    public Result exceptionHandler(HttpServletRequest request, Exception exception) throws Exception {    
        String message =request.getRequestURL().toString();
        if(exception instanceof NotLoginException) {
        	return Result.result(CodeMsg.NOLOGIN);
        }else if(exception instanceof NotPermissionException) {
        	return Result.result(CodeMsg.NORIGHT);
        }
        log.error("发生异常>>"+message,exception);
        return Result.result(CodeMsg.EXCEPTION);
    }
}